﻿//-----------------------------------------------------------------------
// <copyright file="DownloadOptions.cs" company="Sondre Bjellås">
// This software is licensed as Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using FlickrDownloadr.Services;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace FlickrDownloadr
{
    public partial class DownloadOptions : UserControl, IWizardPage
    {
        public void ReloadPage()
        { }

        public DownloadOptions()
        {
            InitializeComponent();
        }

        public Bitmap Icon
        {
            get { return Properties.Resources.Folder_Open; }
        }

        public event NextDelegate Next;

        public void OnNext()
        {
            if (Next != null)
                Next(this, false);
        }

        public string Title
        {
            get { return Properties.Resources.PageOptionsTitle; }
        }
        public string Description
        {
            get { return Properties.Resources.PageOptionsDescription; }
        }

        private void buttonFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.SelectedPath = textBox1.Text;

            DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                DirectoryInfo folder = new DirectoryInfo(folderBrowserDialog.SelectedPath);
                if (!folder.Exists)
                    folder.Create();

                PhotoService.Instance.SearchOptions.Path = folder.FullName;
                textBox1.Text = folder.FullName;
            }
        }

        private void buttonMoveNext_Click(object sender, EventArgs e)
        {
            // Instead of checking if the folder exists, we'll just show a dialog for individual files.

            //bool folderExists = Directory.Exists(textBox1.Text);

            //if (folderExists)
            //{
            //    var dialog = new TaskDialog();
            //    dialog.Text = "The folder already exists. Do you want to save photos into this folder?";
            //    var result = dialog.Show();
            //}

            PhotoService.Instance.SearchOptions.Path = textBox1.Text;

            OnNext();
        }

        private void DownloadOptions_Load(object sender, EventArgs e)
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Flickr");

            path = Path.Combine(path, Utility.RemoveIllegalCharacters(PhotoService.Instance.SearchOptions.Text));

            textBox1.Text = path;
            PhotoService.Instance.SearchOptions.Path = path;
        }
    }
}
